﻿--[[
@brief      通用的浏览器组件
@date       2017-02-26
@author     jameszhai
]]

TBrowserCtrl = 
{
    
    InitCtrl = function()
        ierd_tgp.tpf_ui.Require("Common/tgp_log.lua");
        ierd_tgp.tpf_ui.Require("comm_browser/view.lua");

        TBrowserView.InitBrowser();

        -- 显示浏览器
        comm_center.reg_service("SvrShowBrowser", "TBrowserCtrl.OnShowBrowser");

        -- 显示通用的web弹窗，页面也会调用这个服务
        comm_center.reg_service("SvrShowXinYue", "TBrowserCtrl.OnShowXinYue");

        -- 注册facebook登陆
        comm_center.reg_service("SvrShowFacebook", "TBrowserCtrl.OnShowFaceBookLogin");

        -- 注册打开分区
        comm_center.reg_service("SvrShowChangearea", "TBrowserCtrl.OnShowChangearea");

        -- 显示错误页面
        comm_center.reg_service("SvrShowFBLoginFailed", "TBrowserCtrl.OnShowFBLoginFailed");

        -- 显示FB绑定页面
        comm_center.reg_service("SvrShowFBBindPage", "TBrowserCtrl.OnShowFBBindPage");

        -- 关闭窗体
        comm_center.reg_service("SvrCloseBrowser", "TBrowserCtrl.CloseBrowser");
    end;

    -- 卸载
    UnInitCtrl = function()
        TBrowserView.UnInitBrowser();
    end;

    CloseBrowser = function()
        TBrowserView.BeforeClose();
    end;

    -- 显示浏览器
    OnShowBrowser = function(zParam)
        if nil ~= zParam["WebUrl"] then
            TBrowserView.ShowView(zParam["WebUrl"], zParam);
        else
            TGPLog.LogE("BrowserCtrl", "Show browser, parameter error.");
        end;
    end;

    OnShowFBBindPage = function (zParam)
        local show_param = {};

        local login_mgr =
            base_utility.get_component_adapter("ILogin_mgr",
                "ierd_tgp.login_mgr.GetLoginMgr();");
        local max_nick_len = 1024;
        local nick = NewStr(max_nick_len);
        login_mgr:GetThirdNickName(nick, max_nick_len);

        show_param.WebUrl = LoadStr("FB_BIND_PAGE_URL")..nick;
        show_param.Title = LoadStr("LOGIN_FB_NOTBIND_ERR_TITLE");

        TGPLog.LogE("OnShowFBBindPage", "url:" .. show_param.WebUrl);

        local tpf_ui =
        base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_LOGIN_DLG);
        if component == nil then
            return;
        end

        local rc = component:GetRectEx(TRUE,TRUE);
        local pt = POINT:new_local();
        pt.x = rc.left;
        pt.y = rc.top;
        local rcwork = RECT:new_local();
        tpf_ui:GetMonitorInfo(pt, rcwork);

        show_param.Width = (rcwork.right - rcwork.left)*4/5;
        show_param.Height = (rcwork.bottom - rcwork.top)*4/5;
     

        show_param.canWebClose = false;
        show_param.FixSize = true;
        comm_center.call_service("SvrShowBrowser", show_param);
    end;

    OnShowFaceBookLogin = function(zParam)
        local show_param = {};

        if TBrowserView.sFBWebURL == nil then
            show_param.WebUrl = LoadStr("FACEBOOK_URL") .. EWebUIConst.FB_SUCCEESS_WEB_URL.."&state={st=state123abc,ds=123456789}&locale=";
            local tpfui =  base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
            local language = nil;
            local language_ver = tpfui:get_current_language();
            if language_ver == 0 then
                language = "zh_CN";
            elseif language_ver == 1 then
                language = "zh_TW";
            elseif language_ver == 2 then
                language = "en_US";
            end
            show_param.WebUrl = show_param.WebUrl..language;
            TBrowserView.sFBWebURL = show_param.WebUrl;
        else
            show_param.WebUrl = TBrowserView.sFBWebURL;
        end;

        show_param.Title = "Facebook login";

        local tpf_ui =
        base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_LOGIN_DLG);
        if component == nil then
            return;
        end

        local rc = component:GetRectEx(TRUE,TRUE);
        local cur_resolution_x = 0;
        local cur_resolution_y = 0;
        local cur_work_area_x = 0;
        local cur_work_area_y = 0;
        cur_resolution_x, cur_resolution_y, cur_work_area_x, cur_work_area_y = 
             ui_utility.get_screen_resolution();

--        local pt = POINT:new_local();
--        pt.x = rc.left;
--        pt.y = rc.top;
--        local rcwork = RECT:new_local();
--        tpf_ui:GetMonitorInfo(pt, rcwork);

        show_param.Width = (cur_work_area_x)/5 * 3;
        show_param.Height = (cur_work_area_y)/5 * 3;
        if show_param.Width == 0 or show_param.Height == 0 then
            show_param.Width = 1280;
            show_param.Height = 720;
        end

        show_param.HideIcon = true;
        show_param.canWebClose = false;
        show_param.FixSize = true;
        comm_center.call_service("SvrShowBrowser", show_param);
    end;

    OnShowChangearea = function(zParam)
        local show_param = {};
        show_param.WebUrl = LoadStr("CHANGE_AREA_URL");
        show_param.Title = LoadStr("CHANGE_STORE_AREA");
        show_param.Width = 480;
        show_param.Height = 518;
        show_param.canWebClose = false;
        show_param.FixSize = true;
        comm_center.call_service("SvrShowBrowser", show_param);
    end;


    OnShowFBLoginFailed = function(zParam)
        local show_param = {};
        show_param.WebUrl = LoadStr("FACEBOOK_FAILED_URL");

        if zParam ~= nil and zParam["errmsg"] ~= nil then
            show_param.WebUrl = show_param.WebUrl.."?errmsg="..zParam["errmsg"];
        end

        show_param.Title = "Facebook login";
      
        local tpf_ui =
        base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_LOGIN_DLG);
        if component == nil then
            return;
        end

        local rc = component:GetRectEx(TRUE,TRUE);
        local pt = POINT:new_local();
        pt.x = rc.left;
        pt.y = rc.top;
        local rcwork = RECT:new_local();
        tpf_ui:GetMonitorInfo(pt, rcwork);

        show_param.Width = (rcwork.right - rcwork.left)/5 * 3;
        show_param.Height = (rcwork.bottom - rcwork.top)/5 * 3;
        if show_param.Width == 0 or show_param.Height == 0 then
            show_param.Width = 1280;
            show_param.Height = 720;
        end

        show_param.HideIcon = true;
        show_param.canWebClose = false;
        show_param.FixSize = true;
        comm_center.call_service("SvrShowBrowser", show_param);
    end;

    -- 显示通用web窗体
    OnShowXinYue = function(zParam)
        local   sTemp = LoadStr("XINYUE_URL");

        if (nil ~= zParam["url"]) and (#zParam["url"] > 0) then
            sTemp = zParam["url"];
        end;

        local   zTemp = {};

        if (nil ~= zParam["width"]) then
            zTemp["Width"] = tonumber(zParam["width"]);
        else
            zTemp["Width" ] = 630;    
        end;

        if (nil ~= zParam["height"]) then
            zTemp["Height"] = tonumber(zParam["height"]);
        else
            zTemp["Height"] = 527;
        end;

        if (nil ~= zParam["maxwidth"]) then
            zTemp["MinWidth"] = tonumber(zParam["maxwidth"]);
        else
            zTemp["MinWidth"] = 630;
        end;

        if (nil ~= zParam["maxheight"]) then
            zTemp["MinHeight"] = tonumber(zParam["maxheight"]);
        else
            zTemp["MinHeight"] = 527;
        end;

        if (nil ~= zParam["title"]) then
            zTemp["Title"] = zParam["title"];
        else
            zTemp["Title"] = LoadStr("COMM_BROWSER_ONLINE_SERVICES");
        end;

        -- canWebClose == true : 点击关闭按钮后，回调到web，然后web控制关闭；canWebClose == false ： 直接关闭
        zTemp["canWebClose"] = false;
        if (nil ~= zParam["canWebClose"]) then
            zTemp["canWebClose"] = zParam["canWebClose"];
        end;

        zTemp["FixSize"] = false;
        if (nil ~= zParam["FixSize"]) then
            zTemp["FixSize"] = zParam["FixSize"];
        end;

        local sWebUrl = web_utility.make_login_status_url(sTemp, login_model.main_uin);
        TBrowserView.ShowView(sWebUrl, zTemp);
    end;

};

TBrowserCtrl.InitCtrl();